package com.xianrenit.xianrenpuzi.manager.service.impl;

import com.xianrenit.xianrenpuzi.manager.mapper.OrderStatisticsMapper;
import com.xianrenit.xianrenpuzi.manager.service.OrderStatisticsService;
import com.xianrenit.xianrenpuzi.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {
    @Resource
    OrderStatisticsMapper orderStatisticsMapper;
    @Override
    public void save(OrderStatistics orderStatistics) {
        orderStatisticsMapper.save(orderStatistics);
    }

    @Override
    public Map<String, List> list(String begin, String end) {
        List<OrderStatistics> list = orderStatisticsMapper.selectByDates(begin,end);
        //将list中的每个统计对象的属性值拆分存到不同的list中给前端展示
        List<String> dates = list.stream()
                .map(orderStatistics ->  new DateTime(orderStatistics.getOrderDate())
                        .toString("yyyy-MM-dd")
                ).collect(Collectors.toList());
        List<Integer> totalNums = list.stream().map(OrderStatistics::getTotalNum)
                .collect(Collectors.toList());
        List<BigDecimal> totalAmounts = list.stream().map(OrderStatistics::getTotalAmount)
                .collect(Collectors.toList());
        Map<String, List> map = new HashMap<>();
        map.put("dates",dates);
        map.put("totalNums",totalNums);
        map.put("totalAmounts",totalAmounts);
        return map;
    }
}
